package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.CommonPage;
import cn.outsourcing.supermarket.common.PageResult;
import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import cn.outsourcing.supermarket.common.utils.IPageUtils;
import cn.outsourcing.supermarket.common.utils.JwtUtils;
import cn.outsourcing.supermarket.common.utils.PageResultUtils;
import cn.outsourcing.supermarket.converter.transform.TimestampTransform;
import cn.outsourcing.supermarket.mapper.HiolabsFootprintMapper;
import cn.outsourcing.supermarket.po.HiolabsFootprint;
import cn.outsourcing.supermarket.service.HiolabsFootprintService;
import cn.outsourcing.supermarket.service.HiolabsGoodsService;
import cn.outsourcing.supermarket.vo.FootPrintVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author gzkemays
 * @since 2021-10-23
 */
@Service
public class HiolabsFootprintServiceImpl
    extends ServiceImpl<HiolabsFootprintMapper, HiolabsFootprint>
    implements HiolabsFootprintService {
  @Resource private HiolabsFootprintMapper footprintMapper;
  @Resource private HiolabsGoodsService goodsService;
  @Resource private HttpServletRequest request;

  @Override
  public void addFootPrint(Integer goodsId) {
    Integer userId =
        (Integer) JwtUtils.getTokenClaimByRequest(request, CommonConstants.TOKEN_USER_ID_KEY);
    Integer count =
        footprintMapper.selectCount(
            Wrappers.<HiolabsFootprint>lambdaQuery()
                .eq(HiolabsFootprint::getGoodsId, goodsId)
                .eq(HiolabsFootprint::getUserId, userId));
    // 如果足迹没有该商品记录，则插入足迹。
    if (count == 0) {
      footprintMapper.insert(
          HiolabsFootprint.builder()
              .userId(userId)
              .goodsId(goodsId)
              .addTime(DataUtils.getSeconds())
              .build());
    }
  }

  @Override
  public PageResult<FootPrintVO> getFootPrintVo(CommonPage page) {
    TimestampTransform timestampTransform = new TimestampTransform();
    IPageUtils<HiolabsFootprint> properties = IPageUtils.<HiolabsFootprint>init().properties(page);
    Integer userId =
        (Integer) JwtUtils.getTokenClaimByRequest(request, CommonConstants.TOKEN_USER_ID_KEY);
    // 获取分页数据
    List<HiolabsFootprint> footprints =
        footprintMapper
            .selectPage(
                properties,
                Wrappers.<HiolabsFootprint>lambdaQuery().eq(HiolabsFootprint::getUserId, userId))
            .getRecords()
            .stream()
            .sorted(Comparator.comparing(HiolabsFootprint::getAddTime))
            .collect(Collectors.toList());
    Collections.reverse(footprints);
    // 构造商品
    List<FootPrintVO> footPrints = new ArrayList<>();
    for (HiolabsFootprint footprint : footprints) {
      Integer addTime = footprint.getAddTime();
      boolean isToday = DataUtils.timestampIsToday(addTime);
      footPrints.add(
          FootPrintVO.builder()
              .id(footprint.getId())
              .goodsId(footprint.getGoodsId())
              .addTime(isToday ? "今天" : DataUtils.getTimestampData(addTime))
              .goods(goodsService.getFootGoods(footprint.getGoodsId()))
              .build());
    }
    return PageResultUtils.parseListPageObjs(properties, footPrints);
  }

  @Override
  public boolean deleteFootprint(Integer id) {
    return footprintMapper.deleteById(id) > 0;
  }
}
